<?php

namespace app\admin\model;

use think\Model;


class Livestreamer extends Model
{

    

    

    // 表名
    protected $table = 'livestreamer_list';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'gender_text',
        'contactType_text',
        'signStatus_text',
        'checkStatus_text',
        'grade_text'
    ];

    //百度api
    const API_KEY = "sALESKHmIB4TxRa59B6WFY7O";
    const SECRET_KEY = "7wf4nzoXO4g1WlGUa6jXUXTwfxrDwh5Q";
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['signTime' => $row[$pk]]);
        });
    }

    
    public function getGenderList()
    {
        return ['0' => __('Gender 0'), '1' => __('Gender 1'), '2' => __('Gender 2'), '3' => __('Gender 3')];
    }

    public function getContacttypeList()
    {
        return ['0' => __('Contacttype 0'), '1' => __('Contacttype 1'), '2' => __('Contacttype 2'), '3' => __('Contacttype 3'), '4' => __('Contacttype 4'),'5' => __('Contacttype 5')];
    }

    public function getSignstatusList()
    {
        return ['0' => __('Signstatus 0'), '1' => __('Signstatus 1')];
    }

    public function getCheckstatusList()
    {
        return ['-1' => __('Checkstatus -1'), '0' => __('Checkstatus 0'), '1' => __('Checkstatus 1')];
    }

    public function getGradeList()
    {
        return ['-1' => __('Grade -1'), '0' => __('Grade 0'), '1' => __('Grade 1'), '2' => __('Grade 2')];
    }


    public function getGenderTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
        $list = $this->getGenderList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getContacttypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['contactType']) ? $data['contactType'] : '');
        $list = $this->getContacttypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSignstatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['signStatus']) ? $data['signStatus'] : '');
        $list = $this->getSignstatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCheckstatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['checkStatus']) ? $data['checkStatus'] : '');
        $list = $this->getCheckstatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getGradeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['grade']) ? $data['grade'] : '');
        $list = $this->getGradeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function baiduimgrun($imgurl) {
        $imgbase64 = $this->getFileContentAsBase64($imgurl);
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token={$this->getAccessToken()}",
            CURLOPT_TIMEOUT => 30,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CUSTOMREQUEST => 'POST',
            // image 可以通过 $this.getFileBase64Content("C:\fakepath\020400031014201.jpg") 方法获取
            CURLOPT_POSTFIELDS =>'{"image":"'.$imgbase64.'","image_type":"BASE64","face_field":"age,beauty,gender"}',
    
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json'
            ),

        ));
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }
    
    /**
     * 获取文件base64编码
     * @param string  $path 文件路径
     * @return string base64编码信息，不带文件头
     */
    private function getFileContentAsBase64($path){
        return base64_encode(file_get_contents($path));
    }
    
    /**
     * 使用 AK，SK 生成鉴权签名（Access Token）
     * @return string 鉴权签名信息（Access Token）
     */
    private function getAccessToken(){
        $curl = curl_init();
        $postData = array(
            'grant_type' => 'client_credentials',
            'client_id' => self::API_KEY,
            'client_secret' => self::SECRET_KEY
        );
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://aip.baidubce.com/oauth/2.0/token',
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => http_build_query($postData)
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        $rtn = json_decode($response);
        return $rtn->access_token;
    }




}
